package cn.jeefast.controller;


import cn.jeefast.common.utils.BillNumberBuilder;
import cn.jeefast.entity.Aaa;
import cn.jeefast.entity.Jiekuan;
import cn.jeefast.service.JiekuanService;
import cn.jeefast.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaodongyun
 * @since 2019-01-17
 */
@RestController
@RequestMapping("/jiekuan")
public class JiekuanController {
    @Resource
    JiekuanService service;

    //保存到数据库
    @RequestMapping("/insertjiekuan")
    public String impPriceRecord() throws Exception {
        Jiekuan jiekuan = new Jiekuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String num;
        String path = "";
        Integer integer;
        try {
            path = "C:\\Users\\Wade\\Desktop\\job\\借款excel.xlsx";
            List<List<String>> returnList = ExcelUtil.readExcelWithSheet(new File(path), 0);
            integer = returnList.size();
            System.out.println(returnList.size());
            for (int i = 1; i < returnList.size(); i++) {
                if (returnList.get(i).get(0) == null) {
                    continue;
                }
                jiekuan.setName(returnList.get(i).get(0));
                jiekuan.setBook(returnList.get(i).get(1));
                jiekuan.setPhone(returnList.get(i).get(2));
                jiekuan.setBankcardnumber(returnList.get(i).get(3));
                jiekuan.setCunkuanmoney(Double.valueOf(returnList.get(i).get(4)));
                jiekuan.setCunkuandate(sdf.parse(returnList.get(i).get(5)));
                jiekuan.setLilv(returnList.get(i).get(6));
                jiekuan.setCunkuantype(returnList.get(i).get(7));
                jiekuan.setEnddate(sdf.parse(returnList.get(i).get(8)));
                //jiekuan.setBeizhu(returnList.get(i).get(9));
                if(returnList.get(i).get(9).contains(".")){
                    String asd[]=returnList.get(i).get(9).split("\\.");
                    jiekuan.setJiesuanzhouqi(Integer.valueOf(asd[0]));
                }else{
                    jiekuan.setJiesuanzhouqi(Integer.valueOf(returnList.get(i).get(9)));
                }



                //预计利息  一个季度 ||一年 的利息
                double doublelilv = Double.valueOf(jiekuan.getLilv());
                if (Integer.valueOf(jiekuan.getJiesuanzhouqi()) != 360) {
                    int value = (int) ((jiekuan.getCunkuanmoney() * doublelilv / 30 * 90) * 100);
                    double monry = value / 100;
                    jiekuan.setYujilixi(monry);
                } else if (Integer.valueOf(jiekuan.getJiesuanzhouqi()) == 360) {
                    int value = (int) ((jiekuan.getCunkuanmoney() * doublelilv / 360 * 360) * 100);
                    double monry = value / 100;
                    jiekuan.setYujilixi(monry);
                }
                if (jiekuan.getYujilixi() < 0) {
                    jiekuan.setYujilixi(0.00);
                }
                jiekuan.setJiesuandate(service.yujidate(jiekuan.getEnddate(),jiekuan.getJiesuanzhouqi()));
                jiekuan.setDepartmentid("1");
                jiekuan.setType(0);
                jiekuan.setCaozuoren("系统导入");
                service.insertjiekuan(jiekuan);
                System.out.println(i+1);
            }
        } catch (NumberFormatException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
        //  aaaService.save(aa);
        return "保存数据库成功";
    }


}
